package com.zhuchaooasystem.ui.home.fragment.oa.sign;

import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.MyPageAdapter;
import com.zhuchaooasystem.adapter.holder.SignListHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.base.BaseEvent;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.SignListEntity;
import com.zhuchaooasystem.entity.SignNumEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.presenter.home.fragment.oa.sign.SignContract;
import com.zhuchaooasystem.presenter.home.fragment.oa.sign.SignPresenter;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.NetWorkUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SignActivity extends BaseActivity<SignPresenter> implements SignContract.View, RecyclerArrayAdapter.OnMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.actionbar_iv_back)
    ImageView iv_back;
    @BindView(R.id.actionbar_txt_title)
    TextView txt_title;
    @BindView(R.id.act_sign_txt_name)
    TextView txt_name;
    @BindView(R.id.act_sign_txt_num)
    TextView txt_num;
    @BindView(R.id.act_sign_iv_trail)
    ImageView iv_trail;
    @BindView(R.id.act_sign_vp_sign)
    ViewPager vp_sign;
    @BindView(R.id.act_sign_iv_sign)
    ImageView iv_sign;
    @BindView(R.id.act_sign_tv_sign)
    TextView tv_sign;
    @BindView(R.id.act_sign_iv_sign_record)
    ImageView iv_sign_record;
    @BindView(R.id.act_sign_tv_sign_record)
    TextView tv_sign_record;
    //----
    private UserInfo userInfo;
    //----
    private List<View> viewList;
    private MyPageAdapter adapter;
    //----打卡
    private View view_sign;
    private TextView txt_date;
    private ImageView iv_sign_now;
    //----打卡记录
    private View view_sign_record;
    private int index;
    private EasyRecyclerView recycle_record;
    private RecyclerArrayAdapter signListAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_sign;
    }

    @Override
    protected SignPresenter getPresenter() {
        return new SignPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        userInfo = mPresenter.getLoginInfo();
        index = 1;
        //----
        RxBus.get().register(this);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("signNow")})
    public void signNow(BaseEvent baseEvent) {
        if (null != baseEvent) {
            mPresenter.getSignNum();
            onRefresh();
        }
    }

    @Override
    protected void initView() {
        iv_back.setVisibility(View.VISIBLE);
        txt_title.setText(getString(R.string.actionbar_title_sign));
        //----
        if (null != userInfo) {
            txt_name.setText(userInfo.getUserName());
        }
        //----
        initViewpager();
    }

    private void initViewpager() {
        viewList = new ArrayList<>();
        view_sign = LayoutInflater.from(mContext).inflate(R.layout.activity_sign_vp_sign, null);
        view_sign_record = LayoutInflater.from(mContext).inflate(R.layout.activity_sign_vp_sign_record, null);
        viewList.add(view_sign);
        viewList.add(view_sign_record);
        //----
        setViewSign();
        setViewSignRecord();
        //----
        adapter = new MyPageAdapter(viewList);
        vp_sign.setAdapter(adapter);
        vp_sign.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mPresenter.pagerChanged(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setViewSign() {
        txt_date = (TextView) view_sign.findViewById(R.id.act_sign_vp_sign_txt_date);
        iv_sign_now = (ImageView) view_sign.findViewById(R.id.act_sign_vp_sign_iv_sign);
        iv_sign_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppUtils.jump(mContext, SignNowActivity.class);
            }
        });
    }

    private void setViewSignRecord() {
        recycle_record = (EasyRecyclerView) view_sign_record.findViewById(R.id.act_sign_vp_sign_record_recycle_record);
        recycle_record.setRefreshingColor(Key.RECYCLEVIEW_REFRESH_COLORS);
        recycle_record.setLayoutManager(new LinearLayoutManager(this));
        recycle_record.setAdapterWithProgress(signListAdapter = new RecyclerArrayAdapter<SignListEntity.ResultBean.SuccessfulBean.ListdataBean>(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new SignListHolder(parent);
            }
        });
        recycle_record.setEmptyView(R.layout.view_empty);
        //----
        recycle_record.setRefreshListener(this);
        signListAdapter.setMore(R.layout.view_more, this);
        signListAdapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                signListAdapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                signListAdapter.resumeMore();
            }
        });
        signListAdapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                signListAdapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                signListAdapter.resumeMore();
            }
        });
    }

    @Override
    protected void initDataAndLoadData() {
        mPresenter.getSignNum();
        onRefresh();
    }

    @OnClick({R.id.actionbar_iv_back, R.id.act_sign_iv_trail, R.id.act_sign_linear_sign, R.id.act_sign_linear_sign_record})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.actionbar_iv_back:
                finish();
                break;
            //打卡轨迹
            case R.id.act_sign_iv_trail:
                AppUtils.jump(mContext, SignTrailActivity.class);
                break;
            //打卡
            case R.id.act_sign_linear_sign:
                mPresenter.pagerChanged(0);
                break;
            //打卡记录
            case R.id.act_sign_linear_sign_record:
                mPresenter.pagerChanged(1);
                break;
        }
    }

    @Override
    public void setSignNum(SignNumEntity.ResultBean.SuccessfulBean entity) {
        txt_date.setText("当前日期：" + entity.getServicetime());
        txt_num.setText(entity.getCount() + "");
    }

    @Override
    public void setCurrentItem(int position) {
        vp_sign.setCurrentItem(position);
    }

    @Override
    public void radioGroupChanged(int position) {
        if (position == 0) {
            iv_sign.setBackgroundResource(R.drawable.ic_sign_yes);
            tv_sign.setTextColor(getResources().getColor(R.color.color_7f7f7f));
            iv_sign_record.setBackgroundResource(R.drawable.ic_sign_list_no);
            tv_sign_record.setTextColor(getResources().getColor(R.color.color_1bc3ad));
        } else if (position == 1) {
            iv_sign.setBackgroundResource(R.drawable.ic_sign_no);
            tv_sign.setTextColor(getResources().getColor(R.color.color_1bc3ad));
            iv_sign_record.setBackgroundResource(R.drawable.ic_sign_list_yes);
            tv_sign_record.setTextColor(getResources().getColor(R.color.color_7f7f7f));
        }

    }

    @Override
    public void refreshData(List<SignListEntity.ResultBean.SuccessfulBean.ListdataBean> data) {
        signListAdapter.clear();
        signListAdapter.addAll(data);
    }

    @Override
    public void loadMoreData(List<SignListEntity.ResultBean.SuccessfulBean.ListdataBean> data) {
        signListAdapter.addAll(data);
    }


    @Override
    public void onRefresh() {
        //刷新
        if (!NetWorkUtil.isNetConnected(mContext)) {
            signListAdapter.pauseMore();
            return;
        }
        index = 1;
        mPresenter.getSignRecord(index);
    }

    @Override
    public void onMoreShow() {
        index++;
        mPresenter.getSignRecord(index);
    }

    @Override
    public void onMoreClick() {
        mPresenter.getSignRecord(index);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this);
    }
}
